function bin = fig2bin(fig)
  % Converts a Matlab figure into a database-pushable binary "string"
  
    global procLogStr flightLogStr castLogStr
    
    bin = [];
    if ishandle(fig)

        if isempty(get(fig, 'Name')) % if the figure doesn't have a name...
            name = char(randi(26,1,8)+64); %...generate a random one
        else
            name = get(fig, 'Name'); % but if it has a name, use it
        end
        
      % Save the figure
        try
            hgsave(fig, name);
        catch ME
            procLogStr{end+1} = ['fig2bin error: ' getReport(ME) '.  '];
            return
        end
        
      % fopen the just-saved figure file in binary mode
        [b, msg] = fopen([name '.fig'], 'r');
        if b==-1
            procLogStr{end+1} = ['fig2bin error: ' msg '.  '];
            return
        end

      % fread as uint8
        bin = fread(b, inf, '*int8'); % inf reads the whole file
        
      % Clean up
        fclose(b);
        try
            dos(['del ' name '.fig']);
        catch ME
            procLogStr{end+1} = getReport(ME);
        end
        
    end
end